package tom.change.util;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

public class ProgressBar extends JFrame implements ChangeListener,ActionListener
{

	JButton start, stop;  
    JProgressBar bar;  
    JLabel show;  
    JPanel pane, pane1;  
    Timer timer;  
  
    public ProgressBar(String title,JPanel p) {  
        super(title);  
        init(p,title);  
        pack();  
    }  
  
    public void init(JPanel p,String title) {  
        setBounds(300, 100, 300, 100);  
        pane = new JPanel();  
        pane.setLayout(new BorderLayout());  
        p.add(pane);  
  
        start = new JButton("start");  
        start.addActionListener(this);  
        stop = new JButton("stop");  
        stop.addActionListener(this);  
        pane1 = new JPanel();  
        pane1.add(start);  
        pane1.add(stop);  
  
        bar = new JProgressBar();  
        bar.setMinimum(0);  
        bar.setMaximum(100);  
        //bar.setValue(0);  
        bar.setStringPainted(true);  
        bar.addChangeListener(this);  
        bar.setPreferredSize(new Dimension(200, 30));  
        int leave = bar.getValue();  
        show = new JLabel(title+"(10/0)", JLabel.CENTER);  
  
        pane.add(pane1, BorderLayout.NORTH);  
        pane.add(bar, BorderLayout.CENTER);  
        pane.add(show, BorderLayout.SOUTH);  
  
        timer = new Timer(300, this);  
    }  
  
    @Override  
    public void stateChanged(ChangeEvent e) {  
        // TODO Auto-generated method stub  
        int value = bar.getValue();  
        int leave = value/10;  
        if (e.getSource() == bar) {  
        	String text=show.getText();
            show.setText(text.substring(0, text.lastIndexOf("/")+1)+leave+")");  
        }  
    }  
  
    @Override  
    public void actionPerformed(ActionEvent e) {  
        // TODO Auto-generated method stub  
        if (e.getSource() == start) {  
            timer.start();  
        } else if (e.getSource() == stop) {  
            timer.stop();  
            //bar.setValue(0);  
        } else if (e.getSource() == timer) {  
            int value = bar.getValue();  
            if (value <100) {  
                value++;  
                bar.setValue(value);  
            }  
        }  
    }  
  
    
    public JProgressBar getBar() {
		return bar;
	}

	public void setBar(JProgressBar bar) {
		this.bar = bar;
	}
	
	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}

	public static void main(String[] args) {  
    	try {
    		BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			// TODO: handle exception
			   
		}
//    	new ProgressBar("uninstall").setVisible(true);
    }  

}
